#include"preCmpH.h"
#include "UIEvent.h"

BEGIN_NAMESPACE(ui_manager)
BEGIN_NAMESPACE(ui_events)

//========== <Constructors, Destructor, Operators> ==========

UIEvent::UIEvent()
{
	flag_ = 0;
	msg_ = 0;
	auxInfo_ = 0;
	handled_ = false;
}
UIEvent::UIEvent(unsigned short UIMSG_FLAG_)
{
	flag_ = UIMSG_FLAG_;
	msg_ = 0;
	auxInfo_ = 0;
	handled_ = false;
}

//========== <Setters> ==========

void UIEvent::setMouseMsg(int x, int y, int ax, int ay, unsigned short flag, unsigned short button){
	if(msg_ != 0 || auxInfo_ != 0)
	{
		return;
	}
	else
	{
		if( x < ((1 << 15) - 1) && y < ((1 << 15) - 1))
		{
			if(x < 0)
			{
				msg_ = msg_ | ((~x) + 1);
				msg_ |= (1 << 15);
			}
			else
			{
				msg_ = msg_ | x;
			}
			msg_ = msg_ << 16;
			if(y < 0)
			{
				msg_ = msg_ | ((~y) + 1);
				msg_ |= (1 << 15);
			}
			else
			{
				msg_ = msg_ | y;
			}
		}
		if(button < 3 && flag < 3)
		{
			auxInfo_ |= button;
			auxInfo_ = auxInfo_ << 2;
			auxInfo_ |= flag;
			auxInfo_ = auxInfo_ << 28;
		}
		if(ax < 2 << 13 && ay < 2 << 13)
		{
			auxInfo_ |= ax << 14;
			auxInfo_ |= ay;
		}
	}
}
void UIEvent::setKeyboardMsg(unsigned short flag,unsigned short VK_)
{
	if(msg_ != 0 || auxInfo_ != 0)
	{
		return;
	}
	else
	{
		if(flag < 3 && VK_ < 2 << 8)
		{
			msg_ += VK_;
			msg_ = msg_ << 8;
			msg_ += flag;
		}
	}
}
void UIEvent::setRenderMsg(unsigned int parentX,unsigned int parentY,bool render)
{
	if(auxInfo_ != 0)
	{
		return;
	}
	auxInfo_ = auxInfo_ | parentX;
	auxInfo_ = auxInfo_ << 16;
	auxInfo_ = auxInfo_ | parentY;
	if(render)
	{
		msg_ = 1;
	}
	else
	{
		msg_ = 0;
	}
}
void UIEvent::setTickMsg(bool ticked, int interval, int ms){
	if(msg_ != 0 || auxInfo_ != 0)
	{
		return;
	} else {
		if( interval < ~(1 << 31) && interval > 0){
			if( ticked == true){ msg_ = 1 << 31; }
			msg_ += interval;

			auxInfo_ = ms;
		}
	}
}
void UIEvent::setMsgFlag( unsigned short flag ){ flag_ = flag;}
void UIEvent::setHandled( bool handled ){handled_ = handled;}
//========== <Getters> ==========

bool UIEvent::getRender()
{
	if(msg_ == 1)
	{
		return true;
	}
	else
	{
		return false;
	}
}
unsigned short UIEvent::getMouseButton(){
	return ((auxInfo_ >> 30) & 3);
}
unsigned short UIEvent::getMouseButtonState(){
	return ((auxInfo_ >> 28) & 3);
}
unsigned short UIEvent::getKeyboardKey()
{
	return msg_ >> 8;
}
unsigned short UIEvent::getKeyboardKeyState()
{
	return msg_ & 3;
}
bool UIEvent::getTickState(){
	if(msg_ > 0){
		return true;
	}
	return false;
}
int UIEvent::getTickInterval(){
	return msg_ & ~(1 << 31);
}
int UIEvent::getTickTime(){
	return auxInfo_;
}
unsigned short UIEvent::getMsgFlag() const{return flag_;}
bool UIEvent::getHandled() const{return handled_;}
UIPoint UIEvent::getRenderParentPosition()
{
	int x = auxInfo_ >> 16;
	int y = auxInfo_ & ((1 << 16) - 1);
	UIPoint tor(x,y);
	return tor;
}
UIPoint UIEvent::getMousePosition( bool absolute )
{
	if(absolute){
		int x = (auxInfo_ >> 14) & ((1 << 14) - 1);
		int y = auxInfo_ & ((1 << 14) - 1);
		UIPoint tor(x,y);
		return tor;
	}
	int x = 0;
	if( (msg_ & (1 << 31)))
	{
		x = msg_;
		x &= ~(1 << 31);
		x = x >> 15;
		x = -x;
	}
	else
	{
		x = msg_ >> 15;
	}
	int y = msg_ & ((1 << 16) - 1);
	if( (y & ( 1 << 15)))
	{
		y &= ~(1 << 15); 
		y = -y;
	}
	UIPoint tor(x,y);
	return tor;
}

END_NAMESPACE
END_NAMESPACE